<?php

namespace App\Http\Controllers;

use App\Models\Combination;
use App\Models\CommanderAudit;
use App\Models\Good;
use App\Models\GroupGood;
use App\Models\GroupModel;
use App\Models\OrderModel;
use App\Models\UserInfo;
use App\Services\SpellRegiment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GroupController extends Controller
{
    /**
     * 前台展示拼团商品
     */
    public function groupGoodShow()
    {
        $data = GroupGood::where('group_state',1)->get()->toArray();
        return \App\Services\Request::success(200,'渲染成功',$data);
    }

    /**
     * 拼团商品详情展示
     */
    public function groupDetail(Request $request)
    {
        $groupId = $request->get('id');
        $data = GroupGood::where('id',$groupId)->get()->toArray();
        return \App\Services\Request::success(200,'详情渲染成功',$data);
    }


    /**
     * 获取该团是几人团
     */
    public function sunGroup(Request $request)
    {
        $tuan_id = $request->get('tuan_id');
        $res = GroupGood::where('id',$tuan_id)->first();
        return \App\Services\Request::success(200,'获取成功',$res);
    }

    /**
     * 开团
     */
    public function openRegiment(Request $request)
    {
        DB::beginTransaction();
        try {
            //接取前端传过来的参数
            $data = $request->post();
            //取出用户信息判断是否是团长
            $commander_id = UserInfo::where('id',$data['uid'])->first()->toArray();
            if($commander_id['commander_id']!=1){
                return ['code'=>1000,'msg'=>'您不是团长无法开团','data'=>''];
            }else{
                //根据拼团商品查询数据库是否有我要拼的那个商品
                $goods = GroupGood::where('id',$data['group_id'])->first()->toArray();
                //如果没有该拼团商品则返回该商品不存在
                if($goods==''){
                    return ['code'=>1000,'msg'=>'该批团商品不存在','data'=>''];
                }
                //进行拼团商品库存的一个查询
                $sum = GroupGood::where('id',$data['group_id'])->first()->toArray();
                //查询出剩余库存
                $residue_quantity = $sum['residue_quantity'];
                //查询出该团的开团人数
                $group_number = $sum['group_number'];
                if($residue_quantity<$group_number){
                    return \App\Services\Request::fail(1000,'该团库存不足无法开团','');
                }
                //查看用户开团信息
                //获取团长id进行一个查询
                $pinks = Combination::where('uid',$data['uid'])->where('group_id',$data['group_id'])
                    //状态为1说明正在拼团中，状态为2说明拼团已完成，状态为3证明超时也就是未完成
                    ->where('state',1)->first();
//                如果有数据则代表正在拼团中
                if($pinks) {
                    return ['code' => 1000, 'msg' => '正在拼团中', 'data' => ''];
                }
                $duration = $goods['duration'];
                $res = SpellRegiment::SpellRegimentAdd($data,$duration);
                DB::commit();
                if($res){
                    return \App\Services\Request::success(200,'开团成功','');
                }
            }
        }catch (\Exception $exception)
        {
            DB::rollBack();
            return \App\Services\Request::fail('1000','开团失败','');
        }
    }


    /**
     * 用户加入拼团
     */
    public function groupBooking(Request $request)
    {
        try {
            //获取用户参加这个团的信息
            $data = $request->post();
            //判断用户是否存在
            //取出用户的类型，判断用户是不是团长，如果是团长则无法拼团
            $userInfo = UserInfo::where('id',$data['id'])->first();
            if($userInfo==''){
                return \App\Services\Request::fail(1000,'用户不存在','');
            }
            $commander_id = $userInfo['commander_id'];
            if($commander_id==1){
                return \App\Services\Request::fail(1000,'团长无法进行拼团','');
            }
            //查看订单表看看该用户拼没拼过该团，如果拼了则提示已拼过该团
            $order = OrderModel::where('uid',$data['id'])->where('group_id',$data['group_id'])->where('t_id',$data['tid'])->first();
            //取出用户id
            if($order!=''){
                return \App\Services\Request::fail(1000,'应经拼过该商品','');
            }
            //接着判断该团的人数是否满员，未满员则加入拼团，满员则该团已满员
            $combination = Combination::where('uid',$data['tid'])->where('group_id',$data['group_id'])->first();
            if($combination['people']==$combination['group_number']){
                return \App\Services\Request::fail(1000,'该团已满请重新拼团','');
            }
            //从数据库取出价格
            $group = GroupGood::where('id',$data['group_id'])->first();
            $price = $group['group_price'];
            $ser = (new SpellRegiment())->groupBookingAdd($data,$price);
        }catch (\Exception $eception){
            return \App\Services\Request::fail(1000,'拼团失败','');
        }
    }

    /**
     * 获取开团信息展示到页面
     */
    public function openGroupShow(Request $request)
    {
        //获取
        $group_id = $request->get('id');
        $combination = Combination::where('group_id',$group_id)->get();
        if($combination){
            return \App\Services\Request::success(200,'数据渲染成功',$combination);
        }
    }

    /**
     * 获取团长自提点信息
     */
    public function oneselfCarrySite(Request $request)
    {
        $data = $request->all();
        $site = CommanderAudit::where('uid',$data['tid'])->first();
        return \App\Services\Request::success(200,'渲染成功',$site);
    }
}
